#include "grid_common.h"
#include "serial_communication.h"

void christmas(long color, int wait) {
  for(int i=0; i<STRIP_LENGTH; i++){
    int randomNumber = random(0, 2);
    if(randomNumber == 1){
      setStripColors(i, BLANK);
    }else{
      setStripColors(i, color);
    }

  }
  pushPixels();
  delay(wait);
}

int spinColor = 1;
void spin(int spd) { //flashes segments sequentially "Dance Floor Mode"

  spinColor = (spinColor + 1);
  if(spinColor == 9) spinColor = 1;
  rotateLight(spd, numberToColor(spinColor));
}

void rotateLight(int spd, long color) {
  for(int i = 0; i < 5; i++){
    turnOn(i, color);
    pushPixels();
    clearAll();
    pushPixels();
    delay(spd);
  }
}

void tileTester(){ //In Development...
  
  for(int i=1; i<NUMBER_OF_PORTS; i++){
      int values[5] = {8,0,0,2,2};
      if(i==4){
        values[4] = LEFT;
      }if(i == 3){
        values[4] = BOTTOM;
      }
      portWrite(i,values,5);
  }

  delay(1000);
}

void doNothing(){ 
  if(getNewState()){
    clearAll();
    pushPixels();
  }
}

//The Show
long showColor;
boolean showInitialized = false;
void playShow(long mycolor, int pulseSpeed, int wait) { //Randomly pulses tiles around the Grid
  if(!showInitialized){
    delay(random(0,1000));
    showInitialized = true;
  }
  showColor = mycolor;
  if(getStripStates(CIRCLE) == showColor){
          pulseColor(BLANK, pulseSpeed,-1);
  }else if(getStripStates(CIRCLE) != BLANK){
      pulseColor( showColor, pulseSpeed,-1);
  }
  else{
      pulseColor(showColor,pulseSpeed,-1);
  }
  pushPixels();
  delay(wait);
  delay(random(0,400));
  
}

int iState;
int iLastState;

void interactive(long offColor, long onColor, int spd) { //Tile chagnes from color to color from stepped on (Default)
    iLastState = iState;
    if(spd == 0) spd = 5;
    if(readSwitch() == true){
      iState = 1;
      if(iLastState != iState){
        pulseColor(onColor, spd,-1);  
      }
    }
    else{
      iState = 0;
      if(iLastState != iState){
        pulseColor(offColor, spd,-1);  
      }
    }
    if(getNewState() || (getCurrentColor() != offColor && getCurrentColor() != onColor)){
      if(readSwitch()){
          pulseColor(onColor, spd,-1);
        }else{
          pulseColor(offColor, spd,-1);
     }
   }
}

//Jie Ke: update state of myself based on the state of button for simulator,
//or switch for real tile.
void communicationTest() {
  int prevState = getMyState();
  
  if (readSwitch()){
    setMyState(3);
  } else {
    setMyState(1);
  }
  if (getMyState() == 3) {
    turnOn(CIRCLE, getMyColor());
  } else {
    turnOn(CIRCLE, BLANK);
  }
  if(prevState!= getMyState())     pushPixels();
  if (prevState == 1 && getMyState() == 3) {
    broadcastStateColor();
  }
}

//Matthew's Version...wrote it based off of Dad's. Tried to do it without looking.
int center = 0;
int dir_out = 1;
void mysteriousGrowth(long color, int wait){
  if (dir_out == 1) {
    if (center > 6){
      dir_out = 0;
      center = 6;
    }
  }else {
    if (center < 0){
      dir_out = 1;
      center = 0;
    }
  }
  
  switch(center){
    case 0:
      if (dir_out == 1){
        setStripColors(27, color);
        setStripColors(26, color);
        setStripColors(40, color);
        setStripColors(41, color);
      }else{
        setStripColors(27, BLANK);
        setStripColors(26, BLANK);
        setStripColors(40, BLANK);
        setStripColors(41, BLANK);  
      }break;
    case 1:
      if (dir_out == 1){
        setStripColors(28, color);
        setStripColors(25, color);
        setStripColors(39, color);
        setStripColors(42, color);
      }else{
        setStripColors(28, BLANK);
        setStripColors(25, BLANK);
        setStripColors(39, BLANK);
        setStripColors(42, BLANK);
      }break;
    case 2:
      if (dir_out == 1){
        setStripColors(29, color);
        setStripColors(24, color);
        setStripColors(38, color);
        setStripColors(43, color);
      }else{
        setStripColors(29, BLANK);
        setStripColors(24, BLANK);
        setStripColors(38, BLANK);
        setStripColors(43, BLANK);
      }break;
    case 3:
      if (dir_out == 1){
        for(int i=0; i < 20; i++){
          setStripColors(i, color);
        }
        setStripColors(30, color);
        setStripColors(23, color);
        setStripColors(37, color);
        setStripColors(44, color);
      }else{
        for(int i=0; i < 20; i++){
          setStripColors(i, BLANK);
        }
        setStripColors(30, BLANK);
        setStripColors(23, BLANK);
        setStripColors(37, BLANK);
        setStripColors(44, BLANK);
      }break;
    case 4:
      if (dir_out == 1){
        setStripColors(31, color);
        setStripColors(22, color);
        setStripColors(36, color);
        setStripColors(45, color);
      }else{
        setStripColors(31, BLANK);
        setStripColors(22, BLANK);
        setStripColors(36, BLANK);
        setStripColors(45, BLANK);
      }break;
    case 5:
      if (dir_out == 1){
        setStripColors(32, color);
        setStripColors(21, color);
        setStripColors(35, color);
        setStripColors(46, color);
      }else{
        setStripColors(32, BLANK);
        setStripColors(21, BLANK);
        setStripColors(35, BLANK);
        setStripColors(46, BLANK);  
      }break;
    case 6:
      if (dir_out == 1){
        setStripColors(33, color);
        setStripColors(20, color);
        setStripColors(34, color);
        setStripColors(47, color);
      }else{
        setStripColors(33, BLANK);
        setStripColors(20, BLANK);
        setStripColors(34, BLANK);
        setStripColors(47, BLANK);  
      }break;
  }
    pushPixels();
    delay(wait);
    if (dir_out == 1)
      center++;
    else center--;
  
}

int mattsLED = 0;
void matthewsFlashingFrenzy(long color, int wait){
  if(mattsLED>=STRIP_LENGTH)
    mattsLED=0;
  for(int i=0; i<STRIP_LENGTH; i++){
    if(i == mattsLED){
      setStripColors(i, color);
    }else{
      setStripColors(i, BLANK);
    }
  }
  pushPixels();
  delay(wait);
  mattsLED++;
}
